package io.wdb.modules.generator.generator.controller.operation;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;

import io.wdb.common.annotation.SysLog;
import io.wdb.common.utils.PageUtils;
import io.wdb.common.utils.Query;
import io.wdb.common.utils.R;
import io.wdb.modules.generator.generator.controller.api.BaseController;
import io.wdb.modules.generator.generator.entity.BookgridInfoEntity;
import io.wdb.modules.generator.generator.entity.BookshelfInfoEntity;
import io.wdb.modules.generator.generator.entity.customer.BookEntityCustomer;
import io.wdb.modules.generator.generator.entity.customer.BookShelfInfoEntityCustomer;
import io.wdb.modules.generator.generator.entity.web.BaseResponse;
import io.wdb.modules.generator.generator.entity.web.BookGridsStatus;
import io.wdb.modules.generator.generator.entity.web.WebResponse;
import io.wdb.modules.generator.generator.service.operation.BookShelfOperationService;

@RestController
@RequestMapping("operation/bookShelf")
public class BookShelfOperationController extends BaseController{
	
	private Logger logger = LoggerFactory.getLogger(BookShelfOperationController.class);
	
	@Autowired
	private BookShelfOperationService bookShelfOperationService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("operation:bookshelf:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<BookShelfInfoEntityCustomer> bookShelfEntityCustomers = bookShelfOperationService.list(query);
		int total = bookShelfOperationService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(bookShelfEntityCustomers, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	/**
	 * 
	 * @Method:getBookGrids
	 * @Description:获取一个书柜的所有书格
	 * @Time 2017年12月28日 下午2:49:32
	 * @author: Tom
	 * @param bookshelfInfoEntity
	 * @return
	 */
	@RequestMapping("/bookgrids")
	@RequiresPermissions("operation:bookshelf:list")
	public BaseResponse getBookGrids(@RequestBody BookshelfInfoEntity bookshelfInfoEntity){
		BaseResponse baseResponse = success();
		try {
			List<BookgridInfoEntity> bookgridInfoEntities = bookShelfOperationService.getBookGridsByShelfCode(bookshelfInfoEntity.getCode());
			baseResponse.setDetail(bookgridInfoEntities);
		} catch (Exception e) {
			baseResponse = failed(e);
			logger.error(e.getMessage());
		}
		return baseResponse;
	}
	/**
	 * 
	 * @Method:updateBookGrid
	 * @Description:更改书柜格子状态
	 * @Time 2017年12月28日 下午2:07:27
	 * @author: Tom
	 * @return
	 */
	@RequestMapping("updateBookGrid")
	@RequiresPermissions("operation:bookshelf:update")
	public BaseResponse updateBookGrid(@RequestBody BookGridsStatus bookGridsStatus){
		BaseResponse baseResponse = success();
		try {
			bookShelfOperationService.updateBookGridStatus(bookGridsStatus);
		} catch (Exception e) {
			baseResponse = failed(e);
			logger.error(e.getMessage());
		}
		return baseResponse;
	}
	/**
	 * 
	 * @Method:openCloseBookGrid
	 * @Description:书格状态设置（维修/正常）
	 * @Time 2017年12月28日 下午5:28:13
	 * @author: Tom
	 * @param bookshelfInfoEntity
	 * @return
	 */
	@Deprecated
	@RequestMapping("openCloseBookGrid")
	@RequiresPermissions("operation:bookshelf:update")
	public BaseResponse openCloseBookGrid(@RequestBody BookGridsStatus bookGridsStatus){
		BaseResponse baseResponse = success();
		try {
			bookShelfOperationService.updateBookGridStatus(bookGridsStatus);
		} catch (Exception e) {
			baseResponse = failed(e);
			logger.error(e.getMessage());
		}
		return baseResponse;
	}
	/**
	 * 
	 * @Method:getBookShelfInfo
	 * @Description:获取一个书柜的相关信息
	 * @Time 2017年12月29日 上午9:36:03
	 * @author: Tom
	 * @param bookshelfInfoEntity
	 * @return
	 */
	@RequestMapping("getBookShelfInfo")
	@RequiresPermissions("operation:bookshelf:list")
	public BaseResponse getBookShelfInfo(@RequestBody BookshelfInfoEntity bookshelfInfoEntity){
		BaseResponse baseResponse = success();
		try {
			BookShelfInfoEntityCustomer bookShelfEntityCustomer = bookShelfOperationService.getBookShelfInfo(bookshelfInfoEntity.getCode());
			baseResponse.setDetail(bookShelfEntityCustomer);
		} catch (Exception e) {
			baseResponse = failed(e);
			logger.error(e.getMessage());
		}
		return baseResponse;
	}
	/**
	 * 
	 * @Method:unlockBookGrid
	 * @Description:书格开关柜
	 * @Time 2018年1月2日 上午11:25:32
	 * @author: Tom
	 * @param bookgridInfoEntity
	 * @return
	 */
	@SysLog("用户开关柜")
	@RequestMapping("unlock")
	@RequiresPermissions("operation:bookshelf:open")
	public BaseResponse unlockBookGrid(@RequestBody BookgridInfoEntity bookgridInfoEntity){
		BaseResponse baseResponse = success();
		try {
			WebResponse webResponse = bookShelfOperationService.unlock(bookgridInfoEntity.getBookgridCode());
			baseResponse.setDetail(webResponse);
		} catch (Exception e) {
			baseResponse = failed(e);
			logger.error(e.getMessage());
		}
		return baseResponse;
	}
	/**
	 * 
	 * @Method:restartBookShelf
	 * @Description:用户重启书柜
	 * @Time 2018年2月23日 下午3:08:48
	 * @author: Tom
	 * @param bookgridInfoEntity
	 * @return
	 */
	@SysLog("用户重启书柜")
	@RequestMapping("restart")
	@RequiresPermissions("operation:bookshelf:restart")
	public BaseResponse restartBookShelf(@RequestBody BookgridInfoEntity bookgridInfoEntity){
		BaseResponse baseResponse = success();
		try {
			WebResponse webResponse = bookShelfOperationService.restart(bookgridInfoEntity.getBookshelfCode());
			baseResponse.setDetail(webResponse);
		} catch (Exception e) {
			baseResponse = failed(e);
			logger.error(e.getMessage());
		}
		return baseResponse;
	}
	/**
	 * 
	 * @Method:checkBookGridAll
	 * @Description:整个书柜盘点
	 * @Time 2018年2月26日 上午10:33:00
	 * @author: Tom
	 * @param bookgridInfoEntity
	 * @return
	 */
	@SysLog("整柜盘点")
	@RequestMapping("checkGridAll")
	@RequiresPermissions("operation:bookshelf:checkAll")
	public BaseResponse checkBookGridAll(@RequestBody BookgridInfoEntity bookgridInfoEntity) {
		BaseResponse baseResponse = success();
		try {
			WebResponse webResponse = bookShelfOperationService.checkBookGridAll(bookgridInfoEntity.getBookshelfCode());
			if(webResponse.getCode()==100) {
				webResponse.setMessage(bookgridInfoEntity.getBookshelfCode()+"号书柜盘点成功");
			}
			baseResponse.setDetail(webResponse);
		} catch (Exception e) {
			baseResponse = failed(e);
			logger.error(e.getMessage());
		}
		return baseResponse;
	}
	/**
	 * 
	 * @Method:getBookGridInfo
	 * @Description:书籍中书柜监控,获取各个书格中的所有书籍
	 * @Time 2017年12月29日 上午10:02:33
	 * @author: Tom
	 * @return
	 */
	@RequestMapping("getBookGridInfo")
	@RequiresPermissions("operation:bookshelf:monit")
	public R getBookGridInfo(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<BookEntityCustomer> bookShelfEntityCustomers =  bookShelfOperationService.getBookGridInfo(query);
		PageInfo<BookEntityCustomer> page = new PageInfo<BookEntityCustomer>(bookShelfEntityCustomers);
		int total = (int) page.getTotal();
		
		PageUtils pageUtil = new PageUtils(bookShelfEntityCustomers, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	/**
	 * 
	 * @Method:getBookGridInfoRefresh
	 * @Description:重新盘点书格，并获取该书格所有书籍
	 * @Time 2018年1月4日 下午5:33:31
	 * @author: Tom
	 * @param params
	 * @return
	 */
	@RequestMapping("getBookGridInfoRefresh")
	@RequiresPermissions("operation:bookshelf:refresh")
	public BaseResponse getBookGridInfoRefresh(@RequestBody BookgridInfoEntity bookgridInfoEntity) {
		BaseResponse baseResponse = success();
		try {
			WebResponse webResponse = bookShelfOperationService.checkBookGridOne(bookgridInfoEntity.getBookgridCode());
			if(webResponse.getCode()==100) {
				webResponse.setMessage(bookgridInfoEntity.getBookgridCode()+"号书格盘点成功");
			}
			baseResponse.setDetail(webResponse);
		}catch (Exception e) {
			baseResponse = failed(e);
			logger.error(e.getMessage());
		}
		return baseResponse;
	}
}
